<template>
	<div>
		<el-dialog title="添加" width="600px" class="icon-dialog" :visible.sync="show" @open="open" :before-close="closeForm" append-to-body>
			<el-form :size="size" ref="form" :model="form" :rules="rules" :label-width="$store.getters.device !== 'mobile'?'16%':'90px'">
				 <el-row >
					<el-col :span="24">
						<el-form-item label="用户姓名" prop="name">
							<el-input  v-model="form.name" autoComplete="off" clearable  placeholder="请输入用户姓名"></el-input>
						</el-form-item>
					</el-col>
				 </el-row>
				 <el-row >
					<el-col :span="24">
						<el-form-item label="用户名" prop="user">
							<el-input  v-model="form.user" autoComplete="off" clearable  placeholder="请输入用户名"></el-input>
						</el-form-item>
					</el-col>
				 </el-row>
				<el-row>
					<el-col :span="24">
						<el-form-item label="密码" prop="pwd">
							<el-input  show-password autoComplete="off" v-model="form.pwd"  clearable placeholder="请输入密码"/>
						</el-form-item>
					</el-col>
				</el-row>
        <el-row >
          <el-col :span="24">
            <el-form-item label="金蝶账户" prop="user">
              <el-input  v-model="form.kingdee_account" autoComplete="off" clearable  placeholder="请输入金蝶账号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="金蝶密码" prop="pwd">
              <el-input  show-password autoComplete="off" v-model="form.kingdee_password"  clearable placeholder="请输入金蝶密码"/>
            </el-form-item>
          </el-col>
        </el-row>

				<el-row >
					<el-col :span="24">
						<el-form-item label="所属角色" prop="role_id">
							<el-select style="width:100%" v-model="form.role_id" filterable clearable placeholder="请选择">
								<el-option v-for="(item,i) in role_ids" :key="i" :label="item.key" :value="item.val"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
				 <el-row >
					<el-col :span="24">
						<el-form-item label="备注" prop="note">
							<el-input  v-model="form.note" autoComplete="off" clearable  placeholder="请输入备注"></el-input>
						</el-form-item>
					</el-col>
				 </el-row>
				<el-row >
					<el-col :span="24">
						<el-form-item label="状态" prop="status">
							<el-switch :active-value="1" :inactive-value="1" v-model="form.status"></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
				 <el-row >	
					<el-form-item label="谷歌密钥" prop="google_secret">
					   <el-col :span="16">
							<el-input  v-model="form.google_secret" autoComplete="off" clearable  placeholder="请输入谷歌密钥"></el-input>
					   </el-col>
					   <el-col :span="6">
                            <el-button  @click="getGoogleKey" size="small" type="info">获取密钥</el-button>
                      </el-col>
				    </el-form-item>	
				 </el-row>
				 <el-row >
					<el-col :span="24">
						<el-form-item label="是否客服" prop="is_caller">
							<el-radio-group v-model="form.is_caller">
                                <el-radio :label="1">是</el-radio>
                                <el-radio :label="0">否</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				 </el-row>
        <el-row >
          <el-col :span="24">
            <el-form-item label="流水号标识" prop="serial">
              <el-input  v-model="form.serial" autoComplete="off" clearable  placeholder="请输入流水号标识"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="仓库" prop="warehouse_ids">
              <el-select
                  v-model="form.stockNoList"
                  multiple
                  filterable
                  clearable
                  placeholder="请选择仓库"
                  style="width: 100%">
                <el-option
                    v-for="(item, index) in warehouseList"
                    :key="index"
                    :label="item.text"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="车间" prop="warehouse_ids">
              <el-select
                  v-model="form.produceList"
                  multiple
                  filterable
                  clearable
                  placeholder="请选择车间"
                  style="width: 100%">
                <el-option
                    v-for="(item, index) in houseList"
                    :key="index"
                    :label="item.text"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button :size="size" :loading="loading" type="primary" @click="submit" >
					<span v-if="!loading">确 定</span>
					<span v-else>提 交 中...</span>
				</el-button>
				<el-button :size="size"  @click="closeForm">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
import { add, getFieldList,getGoogleKey } from '@/api/admin/adminuser'
import {getStockList,getHouseList} from "@/api/app/warehouse";

export default {
	name:'adminuseradd',
	components: {
	},
	props: {
		show: {
			type: Boolean,
			default: false
		},
		size: {
			type: String,
			default: 'small'
		},
	},
	data(){
		return {
			form: {
			   name:'',
			   user:'',
			   pwd:'',
			   role_id:0,
			   note:'',
			   status:0,
			   google_secret:'',
			   is_caller:0,
			},
      warehouseList:[],
      houseList:[],
			role_ids:[],
			loading:false,
			rules: {
			}
		}
	},
	watch:{
		show(val){
			if(val){
				getFieldList().then(res => {
					if(res.status == 200){
						this.role_ids = res.data.role_ids
					}
				})
        getStockList({}).then(res => {
          if (res.status === 200) {
            this.warehouseList = res.data.dataList
          }
        })

        getHouseList({}).then(res => {
          if (res.status === 200) {
            this.houseList = res.data.dataList
          }
        })
			}
		}
	},
	methods: {
		open(){
		},
        getGoogleKey(){
           getGoogleKey().then(res=>{
			   this.form.google_secret=res.googleKey
		   })
		},
		submit(){
			this.$refs['form'].validate(valid => {
				if(valid) {
					this.loading = true
          this.form.stockNoList = this.form.stockNoList.join(',')
					add(this.form).then(res => {
						if(res.status == 200){
							this.$message({message: '操作成功', type: 'success'})
							this.$emit('refresh_list')
							this.closeForm()
						}
					}).catch(()=>{
						this.loading = false
					})
				}
			})
		},
		closeForm(){
			this.$emit('update:show', false)
			this.loading = false
			if (this.$refs['form']!==undefined) {
				this.$refs['form'].resetFields()
			}
		},
	}
}
</script>
<style scoped lang="scss">
@import '@/assets/scss/common.scss';
</style>
